﻿using LeEdu.DAL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LeEdu.MODEL;
using System.Data.Entity;


namespace LeEdu.Web.Controllers
{
    public class TradeController : Controller
    {
        /// <summary>
        /// Create Sunshine 3:24
        /// </summary>
       //实例化上下文
        DbContextEntity entity = new DbContextEntity();
        /// <summary>
        /// 财务 Sunshine 
        /// </summary>
        /// <returns></returns>
        public ActionResult finance(int id=1)
        {
            if (Request["Starttime"]!=null)
            {
                ViewBag.StarTime = Request["Starttime"];
            }
            
            DateTime Starttime = Convert.ToDateTime(ViewBag.StarTime);
            if (Request["Starttime"] != null)
            {
                ViewBag.EndTime = Request["Endtime"];
            }
           
            DateTime Endtime = Convert.ToDateTime(ViewBag.EndTime);
            if (Request["Starttime"] != null)
            {
                ViewBag.nickname = Request["nickname"];
            }
            string nickname = ViewBag.nickname;
            List<BllTable> bll= entity.BllTable.Where(p=>p.B_Type=="现金").OrderByDescending(p=>p.B_Date).ToList();
            List<BllTable> listbll= bll.Where(p => (nickname == null ? true : p.B_Name.Contains(nickname) && (Starttime == null ? true : p.B_Date > Starttime) && (Endtime == null ? true : p.B_Date < Endtime))).ToList();
            
            //统计收入
            decimal sumshou = bll.Where(p => p.B_Money > 0).Sum(p => p.B_Money);
            ViewBag.sumshou = sumshou;
            //统计支出
            decimal sumzhi = bll.Where(p => p.B_Money < 0).Sum(p => p.B_Money);
            ViewBag.sumzhi = sumzhi;

            double count = listbll.Count;
            double pagecount = Math.Ceiling(count / 10);
            ViewBag.pagecount = pagecount;
            ViewBag.index=id;
            return View(listbll.OrderByDescending(p=>p.B_Date).Skip((id-1)*10).Take(10).ToList());
        }
        /// <summary>
        /// 虚拟币管理 Sunshine
        /// </summary>
        /// <returns></returns>
        public ActionResult virtualcoin(int id=1)
        {
            if (Request["Starttime"] != null)
            {
                ViewBag.StarTime = Request["Starttime"];
            }

            DateTime Starttime = Convert.ToDateTime(ViewBag.StarTime);
            if (Request["Starttime"] != null)
            {
                ViewBag.EndTime = Request["Endtime"];
            }

            DateTime Endtime = Convert.ToDateTime(ViewBag.EndTime);
            if (Request["Starttime"] != null)
            {
                ViewBag.nickname = Request["nickname"];
            }
            string nickname = ViewBag.nickname;
            List<BllTable> bll = entity.BllTable.Where(p => p.B_Type == "虚拟币").OrderByDescending(p => p.B_Date).ToList();
            List<BllTable> listbll = bll.Where(p => (nickname == null ? true : p.B_Name.Contains(nickname) && (Starttime == null ? true : p.B_Date > Starttime) && (Endtime == null ? true : p.B_Date < Endtime))).ToList();
            //统计总余额
            decimal sum = bll.Sum(p => p.B_Money);
            ViewBag.sum = sum;
            //统计充值
            decimal sumshou = bll.Where(p => p.B_Money > 0).Sum(p => p.B_Money);
            ViewBag.sumshou = sumshou;
            //统计收入
            decimal sumzhi = bll.Where(p => p.B_Money < 0).Sum(p => p.B_Money);
            ViewBag.sumzhi = sumzhi;
            double count = listbll.Count;
            double pagecount = Math.Ceiling(count / 10);
            ViewBag.pagecount = pagecount;
            ViewBag.index = id;
            return View(listbll.OrderByDescending(p => p.B_Date).Skip((id - 1) * 10).Take(10).ToList());
        }
        /// <summary>
        /// 课程退款管理 Sunshine
        /// </summary>
        /// <returns></returns>
        public ActionResult courserefund(int id=1)
        {
            if (Request["Starttime"] != null)
            {
                ViewBag.nickname = Request["nickname"];
            }
            string nickname = ViewBag.nickname;
            List<Refund> classrefundlist = entity.Refund.Where(p => (p.Type == "课程") && (nickname == null||nickname=="" ? true : p.UserInfo.Uname.Contains(nickname))).ToList();
            double count = classrefundlist.Count;
            double pagecount = Math.Ceiling(count / 10);
            ViewBag.pagecount = pagecount;
            ViewBag.index = id;
            return View(classrefundlist.OrderByDescending(p => p.Date).Skip((id - 1) * 10).Take(10).ToList());
        }
        /// <summary>
        /// 班级退款管理 Sunshine
        /// </summary>
        /// <returns></returns>
        public ActionResult Graderefund(int id=1)
        {
            if (Request["Starttime"] != null)
            {
                ViewBag.nickname = Request["nickname"];
            }
            string nickname=ViewBag.nickname;
            List<Refund> classrefundlist = entity.Refund.Where(p => (p.Type == "班级") && (nickname==null?true:p.UserInfo.Uname.Contains(nickname))).ToList();
            double count = classrefundlist.Count;
            double pagecount = Math.Ceiling(count / 10);
            ViewBag.pagecount = pagecount;
            ViewBag.index = id;
            return View(classrefundlist.OrderByDescending(p => p.Date).Skip((id - 1) * 10).Take(10).ToList());

        }
        /// <summary>
        /// 模态框显示信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult selectrefunce(int id)
        {
            Refund re = entity.Refund.Where(p => p.OID == id).FirstOrDefault();
            return View(re);
        }
        public ActionResult audit(int id)
        {
            Refund re = entity.Refund.Where(p => p.TID == id).FirstOrDefault();
            re.Tstade = "通过";
            entity.Entry<Refund>(re).State = EntityState.Modified;
            if(entity.SaveChanges()>0)
            {
                return Content("成功");
            }
            else
            {
                return Content("失败");
            }  
        }
        /// <summary>
        /// 虚拟币账单 Sunshine
        /// </summary>
        /// <returns></returns>
        public ActionResult virtualbill(int id=1)
        {
            if (Request["Starttime"] != null)
            {
                ViewBag.StarTime = Request["Starttime"];
            }

            DateTime Starttime = Convert.ToDateTime(ViewBag.StarTime);
            if (Request["Starttime"] != null)
            {
                ViewBag.EndTime = Request["Endtime"];
            }

            DateTime Endtime = Convert.ToDateTime(ViewBag.EndTime);
            if (Request["Starttime"] != null)
            {
                ViewBag.nickname = Request["nickname"];
            }
            string nickname = ViewBag.nickname;
            List<BllTable> bll = entity.BllTable.Where(p => p.B_Type == "虚拟币").OrderByDescending(p => p.B_Date).ToList();
            List<BllTable> listbll = bll.Where(p => (nickname == null ? true : p.B_Name.Contains(nickname) && (Starttime == null ? true : p.B_Date > Starttime) && (Endtime == null ? true : p.B_Date < Endtime))).ToList();
            //统计总余额
            decimal sum = bll.Sum(p => p.B_Money);
            ViewBag.sum = sum;
            //统计充值
            decimal sumshou = bll.Where(p => p.B_Money > 0).Sum(p => p.B_Money);
            ViewBag.sumshou = sumshou;
            //统计收入
            decimal sumzhi = bll.Where(p => p.B_Money < 0).Sum(p => p.B_Money);
            ViewBag.sumzhi = sumzhi;
            double count = listbll.Count;
            double pagecount = Math.Ceiling(count / 10);
            ViewBag.pagecount = pagecount;
            ViewBag.index = id;
            return View(listbll.OrderByDescending(p => p.B_Date).Skip((id - 1) * 10).Take(10).ToList());

        }
    }
}
